﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using Protocol.dto;
using System.Diagnostics;
using Atom.NetFrame;
using Atom.Tool;

namespace Atom.Logic.Fight
{
    public class FightHandler : AbsMulitHandle, HandleInterface
    {

        /// <summary>
        /// UserId 2 Room
        /// </summary>
        ConcurrentDictionary<int, int> userRoom = new ConcurrentDictionary<int, int>();
        /// <summary>
        /// 房间id与模型映射
        /// </summary>
        ConcurrentDictionary<int, FightRoom> roomMap = new ConcurrentDictionary<int, FightRoom>();
        /// <summary>
        /// 回收利用过的房间对象再次利用，减少gc性能开销
        /// </summary>
        ConcurrentStack<FightRoom> cache = new ConcurrentStack<FightRoom>();
        /// <summary>
        /// 区域码
        /// </summary>
        ConcurrentInteger index = new ConcurrentInteger();

        private bool m_IsRunning = false;

        public FightHandler()
        {
            EventUtil.CreateFight += Create;
            EventUtil.DestoryFight += Destory;

            //新的线程来tick
            Task task = Task.Factory.StartNew(TimeDown);
            m_IsRunning = true;


            CreateTestRoom();
        }



        public void Destory(int roomId)
        {
            FightRoom room;
            if (roomMap.TryRemove(roomId, out room))
            {

            }
        }

        public void ClientClose(UserToken ut, string error)
        {
            Leave(ut);
        }

        public void ClientConnect(UserToken ut)
        {

        }
        public void MessageReceive(UserToken ut, SocketModel message)
        {
            //roomMap[1].MessageReceive(ut, message);
            testRoom.MessageReceive(ut, message);
        }


        /// <summary>
        /// 创建房间
        /// </summary>
        public void Create(List<int> team)
        {
            FightRoom room = new FightRoom();
            room.Init(team);
            int area = index.GetAndAdd();
            room.SetArea(area);
            roomMap.TryAdd(area, room);
            for (int i = 0; i < team.Count; i++)
            {
                userRoom.TryAdd(team[i], area);
            }
            Console.WriteLine("-------------------------------创建房间------------------------------");
        }

        public void Leave(UserToken ut)
        {
            //int userID = UserBiz.GetUser(ut).ID;
            //if (userRoom.ContainsKey(userID))
            //{
            //    int roomID = userRoom[userID];
            //    FightRoom room = roomMap[roomID];
            //    bool isEmpty = room.LevelGame(userID);
            //    if (isEmpty)
            //    {
            //        roomMap.TryRemove(roomID, out room);
            //    }
            //    userRoom.TryRemove(userRoom[userID], out roomID);
            //}
            Console.WriteLine("游戏中离开-------------------------------------------------------------");
        }

        /// <summary>
        /// 倒计时
        /// </summary>
        private void TimeDown()
        {
            Stopwatch stop = new Stopwatch();
            stop.Start();
  
            // 5毫秒tick一次
            long timer = 5;
            while (m_IsRunning)
            {
                long curTime = stop.ElapsedMilliseconds;
                if (curTime >= timer)
                {
                    timer += 5;
                    //foreach (var item in roomMap.Values)
                    //{
                    //    item.DoTick();
                    //}
                    testRoom.DoTick();
                }
            }
        }


        //测试房间
        private TestFightRoom testRoom;

        /// <summary>
        /// 创建测试房间
        /// </summary>
        private void CreateTestRoom()
        {
            Console.WriteLine("-------------------------------创建测试房间------------------------------");
            testRoom = new TestFightRoom();
            testRoom.Init();
        }
    }
}
